<?php

namespace app\admin\controller\common;

use app\admin\controller\Base;
use think\Request;
use app\admin\service\common\TablesService;
class Tables extends Base
{
    //数据表
    public function list(Request $request){
      
        $param = $request->param();
        $service = new TablesService();
        $data = $service->getList($param);
        return $data;
    }
    //备份数据库
    public function backup(Request $request){
        $param = $request->param();
        $Service = new TablesService();
        $result = $Service->backup($param);
        if (isset($result['sql']) && isset($result['fileName'])) {
            while (ob_get_level() > 0) {
                ob_end_clean();
            }
            
            return response($result['sql'], 200, [
                'Content-Type' => 'application/octet-stream',
                'Content-Disposition' => 'attachment; filename="' . $result['fileName'] . '"',
                'Content-Encoding' => 'identity'
            ]);
        } else {
            return json($result);
        }
    }
    //执行SQL语句
    public function sql(Request $request){
        $param = $request->param();
        $Service = new TablesService();
        $data = $Service->tableSQL($param);
        return $data;
    }
    //数据库删除/优化/清理/修复
    public function batch(Request $request){
        $param = $request->param();
        $ids = is_array($param['ids']) ? $param['ids'] : explode(',', $param['ids']);
        $Service = new TablesService();
        $data = $Service->batch($ids,$param['type']);
        return $data;
    }
}
